﻿using DemountCIM.Framework.DataBase;
using DemountCIM.Framework.DataBase.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DemountCIM.Windows
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();

            this.Pwd.Focus();
        }

        private void Pwd_KeyUp(object sender, KeyEventArgs e)
        {
            string strName = this.userCbx.SelectedItem?.ToString();
            string strUserPW = this.Pwd.Password;
            var userRepository = DataBaseContainer.GetInstance().GetUserRepository();
            if (userRepository.UserLogin(strName, strUserPW))
            {
                TbUser user = new TbUser();
                userRepository.GetCurUser(ref user);
                switch (user.Level)
                {
                    case UserLevel.管理员:
                        {

                            break;
                        }
                    case UserLevel.工程师:
                        {
                            break;
                        }
                    case UserLevel.操作工:
                        {

                            break;
                        }
                    case UserLevel.无效:
                        {

                            break;
                        }
                    default:
                        break;
                }
                this.Pwd.Clear();
                this.Close();
            }
        }

        private void userCbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.Pwd.Clear();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyboardDevice.IsKeyDown(Key.Escape))
                this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            userCbx.Items.Clear();
            userCbx.ItemsSource = DataBaseContainer.GetInstance().GetUserRepository().AsQueryable().Select(a => a.Name).ToPageList(0, 20);
            userCbx.SelectedIndex = 0;
        }
    }
}
